package capstoneproject.dudiachi.Main;

import java.util.ArrayList;

import capstoneproject.dudiachi.Controller.FestivalManager;
import capstoneproject.dudiachi.Controller.IslandArticleManager;
import capstoneproject.dudiachi.Controller.OldDistrictManager;
import capstoneproject.dudiachi.Controller.PlaceManager;
import capstoneproject.dudiachi.Model.Festival;
import capstoneproject.dudiachi.Model.IslandArticle;
import capstoneproject.dudiachi.Model.OldDistrict;
import capstoneproject.dudiachi.Model.Place;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class DuDiaChiFragment extends Fragment implements OnClickListener {

	private TextView contentBot;
	private TextView titleBot;
	private TextView titleTop;
	private ImageView bgImage;
	private Context context;
	private String title;
	private ArrayList<Bitmap> bitmap;
	private String ID;
	private String name;
	private String des;
	private String PlaceAccessMethod;
	private String FestivalAccessMethod;

	public DuDiaChiFragment(Context context, String title,
			ArrayList<Bitmap> bitArray, String ID, String name, String randomDes) {
		this.context = context;
		this.title = title;
		this.bitmap = bitArray;
		this.ID = ID;
		this.name = name;
		this.des = randomDes;
	}

	public DuDiaChiFragment() {
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.du_dia_chi_fragment, container, false);
		if (container == null) {
			return null;
		}
		titleTop = (TextView) view.findViewById(R.id.titleTop);
		titleTop.setText(title);
		titleTop.setOnClickListener(this);
		bgImage = (ImageView) view.findViewById(R.id.bgImage);
		bgImage.setImageBitmap(bitmap.get(0));
		titleBot = (TextView) view.findViewById(R.id.titleBot);
		titleBot.setText(name);
		contentBot = (TextView) view.findViewById(R.id.contentBot);
		contentBot.setText(des);
		contentBot.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				clickText(v);
				
			}
		});

		return view;

	}

	@Override
	public void onClick(View v) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		FestivalAccessMethod = sharedPref.getString(SettingFragment.TRUY_CAP_LE_HOI, "");
		PlaceAccessMethod = sharedPref.getString(SettingFragment.TRUY_CAP_DIA_DANH, "");
		
		if (title.equals(getResources().getString(R.string.title_diachicandai))) {
			Intent intent = new Intent(context, OldProvinceActivity.class);
			startActivity(intent);
		}
		if (title.equals(getResources().getString(R.string.title_diachilehoi))) {
			if (FestivalAccessMethod.equals("1")) {
				Intent intent = new Intent(context,
						FestivalCalendarActivity.class);
				startActivity(intent);
			}
			if (FestivalAccessMethod.equals("2")) {
				Intent intent = new Intent(context,
						FestivalProvinceActivity.class);
				startActivity(intent);
			}
		}
		if (title.equals(getResources().getString(
				R.string.title_diadanhthamquan))) {
			if (PlaceAccessMethod.equals("1")) {
				Intent intent = new Intent(context, GoogleMapActivity.class);
				startActivity(intent);
			}
			if (PlaceAccessMethod.equals("2")) {
				Intent intent = new Intent(context, ProvinceActivity.class);
				startActivity(intent);
			}
		}
		if (title.equals(getResources().getString(R.string.title_biendaothanthuong))) {
			Intent intent = new Intent(context, IslandArticleTypeActivity.class);
			startActivity(intent);
		}
		
		
	}

	public void clickText(View v){
		if (title.equals(getResources().getString(R.string.title_diachicandai))) {
			OldDistrictManager manager = new OldDistrictManager(context);
			ArrayList<OldDistrict> list = new ArrayList<OldDistrict>();
			list.add(manager.searchByID(ID));
			if(ID.equals(list.get(0).getOldDistrictID())){
				Intent intent = new Intent(context, DetailOldDistrictActivity.class);
				Bundle b = new Bundle();
				b.putSerializable("list", list);
				b.putString("id", ID);
				intent.putExtras(b);
				startActivity(intent);
			}
		}
		if (title.equals(getResources().getString(R.string.title_diachilehoi))) {
			FestivalManager manager = new FestivalManager(context);
			ArrayList<Festival> list = new ArrayList<Festival>();
			list.add(manager.searchByID(ID));
			if(ID.equals(list.get(0).getFestivalID())){
				Intent intent = new Intent(context, DetailFestivalActivity.class);
				Bundle b = new Bundle();
				b.putSerializable("list", list);
				b.putString("id", ID);
				intent.putExtras(b);
				startActivity(intent);
			}
		}
		if (title.equals(getResources().getString(R.string.title_diadanhthamquan))) {
			PlaceManager manager = new PlaceManager(context);
			ArrayList<Place> list = new ArrayList<Place>();
			list.add(manager.searchByID(ID));
			if(ID.equals(list.get(0).getPlaceID())){
				Intent intent = new Intent(context, DetailPlaceActivity.class);
				Bundle b = new Bundle();
				b.putSerializable("list", list);
				b.putString("id", ID);
				intent.putExtras(b);
				startActivity(intent);
			}
		}
		if (title.equals(getResources().getString(R.string.title_biendaothanthuong))) {
			IslandArticleManager manager = new IslandArticleManager(context);
			ArrayList<IslandArticle> list = new ArrayList<IslandArticle>();
			list.add(manager.searchByID(ID));
			if(ID.equals(list.get(0).getIslandArticleID())){
				Intent intent = new Intent(context, DetailIslandArticleActivity.class);
				Bundle b = new Bundle();
				b.putSerializable("list", list);
				b.putString("id", ID);
				intent.putExtras(b);
				startActivity(intent);
			}
		}
	}
}
